﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Minion.Win.Client.IoC;
using Minion.Win.Client.Models;
using Minion.Win.Client.Service;

namespace Minion.Win.Client.Views
{
    public partial class AddLocationItem : PhoneApplicationPage
    {
        public AddLocationItem()
        {
            InitializeComponent();
            BuildLocalizedApplicationBar();
        }

        private void BuildLocalizedApplicationBar()
        {
            // Set the page's ApplicationBar to a new instance of ApplicationBar.
            ApplicationBar = new ApplicationBar();
            var appBarSaveButton =
                new ApplicationBarIconButton(new Uri("/Assets/AppBar/Save.png", UriKind.Relative))
                    {
                        Text = "Save"
                    };
            appBarSaveButton.Click += AppBarSaveButtonOnClick;
            ApplicationBar.Buttons.Add(appBarSaveButton);

            var appBarCancelButton =
                new ApplicationBarIconButton(new Uri("/Assets/AppBar/Cancel.png", UriKind.Relative))
                {
                    Text = "Cancel"
                };
            appBarCancelButton.Click += AppBarCancelButtonOnClick;
            ApplicationBar.Buttons.Add(appBarCancelButton);

        }

        private void AppBarCancelButtonOnClick(object sender, EventArgs eventArgs)
        {
            NavigationService.GoBack();
        }

        private async void AppBarSaveButtonOnClick(object sender, EventArgs eventArgs)
        {
            var locationId = Guid.NewGuid();
            var locationItemId = Guid.NewGuid();
            var locationItemDetailId = Guid.NewGuid();
            var selectedItemType = ItemTypePicker.SelectedItem as ListPickerItem;
            var itemType = selectedItemType.Content;
            var geoposition = await App.Geolocator.GetGeopositionAsync();
            var lat = Math.Round(geoposition.Coordinate.Latitude, 6);
            var lon = Math.Round(geoposition.Coordinate.Longitude, 6);
            
            var location = new Location
                {
                    Id = locationId,
                    Latitude = lat,
                    Longitude = lon,
                    LocationItems = new List<LocationItem>()
                        {
                            new LocationItem()
                                {
                                    Id = locationItemId,
                                    ItemTitle = MainTitleTextBox.Text,
                                    ItemDescription = SmallDescriptionTextBox.Text,
                                    ItemType = GetItemType(itemType.ToString()),
                                    LocationId = locationId,
                                    LocationItemDetails = new List<LocationItemDetail>()
                                        {
                                            new LocationItemDetail()
                                                {
                                                    Id = locationItemDetailId,
                                                    LocationItemId = locationItemId,
                                                    Title = DetailTitleTextBox.Text,
                                                    Description = DetailDescTextBox.Text,
                                                    Email = EmailTextBox.Text
                                                }
                                        }
                                }
                        }
                };
           
            var dataService = Container.Get<IDataService>(App.Mode);
            dataService.SaveLocationItem(location);
            NavigationService.GoBack();

        }

        private static string GetItemType(string selectedItem)
        {
            switch (selectedItem.ToLowerInvariant())
            {
                case "informational":
                    return Constants.ItemType.Information;
                case "notification":
                    return Constants.ItemType.Notification;
            }
            return Constants.ItemType.Information;
        }

        void control_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ShowOrHideControls();
        }

        private void ShowOrHideControls()
        {
            var selItem = ItemTypePicker.SelectedItem as ListPickerItem;
            if(selItem == null) return;
            switch (selItem.Content.ToString().ToLowerInvariant())
            {
                case "informational":
                    EmailLabel.Visibility = Visibility.Collapsed;
                    EmailTextBox.Visibility = Visibility.Collapsed;
                    DetailedTitleLabel.Visibility = Visibility.Visible;
                    DetailTitleTextBox.Visibility = Visibility.Visible;
                    DetailedDescLabel.Visibility = Visibility.Visible;
                    DetailDescTextBox.Visibility = Visibility.Visible;
                    break;
                case "notification":
                    EmailLabel.Visibility = Visibility.Visible;
                    EmailTextBox.Visibility = Visibility.Visible;
                    DetailedTitleLabel.Visibility = Visibility.Collapsed;
                    DetailTitleTextBox.Visibility = Visibility.Collapsed;
                    DetailedDescLabel.Visibility = Visibility.Collapsed;
                    DetailDescTextBox.Visibility = Visibility.Collapsed;
                    break;
            }
        }


        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            ItemTypePicker.SelectionChanged += control_SelectionChanged;
            ShowOrHideControls();
        }
    }
}